/*===========================
nMSXtiles
http://pentacour.com
============================*/

#include "mainwindow.h"
#include "screenw.h"
#include "spritesw.h"
#include <QMenuBar>
#include <QAction>
#include <QDockWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QCloseEvent>
#include <QDebug>
#include "fdlgloadbiblio.h"
#include "dlgsubscreen.h"


CMainWindow::CMainWindow(): QMainWindow( )
{
    m_Version = "0.7.1";

	m_pActNewProject = new QAction( tr( "New" ), this );
	m_pActLoadProject = new QAction( tr( "Load" ), this );
	m_pActSaveProject = new QAction( tr( "Save" ), this );
	m_pActSaveAsProject = new QAction( tr( "Save as..." ), this );
	m_pActCloseProject = new QAction( tr( "Close" ), this );
	m_pActImportPolkaProject = new QAction( tr( "Import Polka! Project" ), this );
	m_pActExportPolkaProject = new QAction( tr( "Export Polka! Project" ), this );
	m_pActQuit = new QAction( tr( "Quit" ), this );
	
	m_pActNewScreen = new QAction( tr( "New" ), this );
	m_pActLoadScreen = new QAction( tr( "Load" ), this );
	m_pActSaveScreen = new QAction( tr( "Save" ), this );
	m_pActSaveAsScreen = new QAction( tr( "Save as..." ), this );
	m_pActExportScreenData = new QAction( tr( "Export ASM data..." ), this );
	m_pActExportScreenDataHexa = new QAction( tr( "Export ASM data Hexadecimal..." ), this );
	m_pActExportScreenBin = new QAction( tr( "Export bin data..." ), this );
	m_pActExportScreenBinPletter = new QAction( tr( "Export bin data compressed with Pletter..." ), this );
    m_pActExportScreenPNG = new QAction( tr( "Export PNG file"), this );
    m_pActFillScreen1to255 = new QAction( tr( "Fill Screen with tiles 1 to 255"), this );
	
	m_pActNewTiles = new QAction( tr( "New" ), this );
	m_pActLoadTiles = new QAction( tr( "Load" ), this );
	m_pActSaveTiles = new QAction( tr( "Save" ), this );
	m_pActSaveAsTiles = new QAction( tr( "Save as..." ), this );
	m_pActLoadTilesLibrary = new QAction( tr( "Load from library..." ), this );
	m_pActExportTilesData = new QAction( tr( "Export ASM data..." ), this );
	m_pActExportTilesDataHexa = new QAction( tr( "Export ASM data Hexadecimal..." ), this );
	m_pActExportTilesBin = new QAction( tr( "Export bin data..." ), this );
	m_pActExportTilesBinPletter = new QAction( tr( "Export bin data compressed with Pletter..." ), this );
    m_pActExportTilesPNG = new QAction( tr( "Export PNG file"), this );
    m_pActImportTilesPNG = new QAction( tr( "Import PNG file created with nMSXtiles"), this );
    m_pActImportTilesPNGAdv = new QAction( tr( "Import PNG without palette"), this );
	m_pActImportTiles = new QAction( tr( "Import..." ), this );
	m_pActGroupTiles = new QAction( tr( "Group Tiles" ), this );
	
	m_pActCopyTiles = new QAction( tr( "Copy" ), this );
	m_pActCopyTiles->setShortcut(tr("Ctrl+C"));
	m_pActPasteTiles = new QAction( tr( "Paste" ), this );
	m_pActPasteTiles->setShortcut(tr("Ctrl+V"));
	m_pActPasteTilesColors = new QAction( tr( "Paste colors" ), this );
	m_pActPasteTilesColors->setShortcut(tr("Ctrl+G"));
	m_pActUndo = new QAction( tr( "Undo" ), this );
	m_pActUndo->setShortcut( tr( "Ctrl+Z" ) );
	
	m_pActNewSprites = new QAction( tr( "New" ), this );
	m_pActLoadSprites = new QAction( tr( "Load" ), this );
	m_pActSaveSprites = new QAction( tr( "Save" ), this );
	m_pActSaveAsSprites = new QAction( tr( "Save as..." ), this );
	m_pActCloseSprites = new QAction( tr( "Close" ), this );
	m_pActExportSprites = new QAction( tr( "Export..." ), this );
	
	m_pActAbout = new QAction( tr( "nMSXtiles" ), this );

	connect( m_pActNewProject, SIGNAL( triggered() ), this, SLOT( OnNewProject() ) );
	connect( m_pActLoadProject, SIGNAL( triggered() ), this, SLOT( OnLoadProject() ) );
	connect( m_pActSaveProject, SIGNAL( triggered() ), this, SLOT( OnSaveProject() ) );
	connect( m_pActSaveAsProject, SIGNAL( triggered() ), this, SLOT( OnSaveAsProject() ) );
	connect( m_pActCloseProject, SIGNAL( triggered() ), this, SLOT( OnCloseProject() ) );
	connect( m_pActImportPolkaProject, SIGNAL( triggered()), this, SLOT( OnImportPolkaProject() ) );
	connect( m_pActExportPolkaProject, SIGNAL( triggered() ), this, SLOT( OnExportPolkaProject() ) );
	connect( m_pActQuit, SIGNAL( triggered() ), this, SLOT( OnQuit() ) );
	
//	connect( m_pActCopy, SIGNAL( triggered() ), this, SLOT( OnCopy() ) );
	//connect( m_pActPaste, SIGNAL( triggered() ), this, SLOT( OnPaste() ) );
	
	connect( m_pActNewScreen, SIGNAL( triggered() ), this, SLOT( OnNewScreen() ) );
	connect( m_pActLoadScreen, SIGNAL( triggered() ), this, SLOT( OnLoadScreen() ) );
	connect( m_pActSaveScreen, SIGNAL( triggered() ), this, SLOT( OnSaveScreen() ) );
	connect( m_pActSaveAsScreen, SIGNAL( triggered() ), this, SLOT( OnSaveAsScreen() ) );
	connect( m_pActExportScreenData, SIGNAL( triggered() ), this, SLOT( OnExportScreenData() ) );
	connect( m_pActExportScreenDataHexa, SIGNAL( triggered() ), this, SLOT( OnExportScreenDataHexa() ) );
	connect( m_pActExportScreenBin, SIGNAL( triggered() ), this, SLOT( OnExportScreenBin() ) );
	connect( m_pActExportScreenBinPletter, SIGNAL( triggered() ), this, SLOT( OnExportScreenBinPletter() ) );
    connect( m_pActExportScreenPNG, SIGNAL( triggered() ), this, SLOT( OnExportScreenPNG() ) );
	connect( m_pActNewTiles, SIGNAL( triggered() ), this, SLOT( OnNewTiles() ) );
	connect( m_pActLoadTiles, SIGNAL( triggered() ), this, SLOT( OnLoadTiles() ) );
	connect( m_pActSaveTiles, SIGNAL( triggered() ), this, SLOT( OnSaveTiles() ) );
	connect( m_pActSaveAsTiles, SIGNAL( triggered() ), this, SLOT( OnSaveAsTiles() ) );
	connect( m_pActLoadTilesLibrary, SIGNAL( triggered() ), this, SLOT( OnLoadTilesLibrary() ) );
	connect( m_pActExportTilesData, SIGNAL( triggered() ), this, SLOT( OnExportTilesData() ) );
	connect( m_pActExportTilesDataHexa, SIGNAL( triggered() ), this, SLOT( OnExportTilesDataHexa() ) );
	connect( m_pActExportTilesBin, SIGNAL( triggered() ), this, SLOT( OnExportTilesBin() ) );
	connect( m_pActExportTilesBinPletter, SIGNAL( triggered() ), this, SLOT( OnExportTilesBinPletter() ) );
    connect( m_pActFillScreen1to255, SIGNAL( triggered() ), this, SLOT( OnFillScreen1to255() ));
    connect( m_pActExportTilesPNG, SIGNAL( triggered() ), this, SLOT( OnExportTilesPNG()) );
    connect( m_pActImportTilesPNG, SIGNAL( triggered()), this, SLOT( OnImportTilesPNG()));
    connect( m_pActImportTilesPNGAdv, SIGNAL( triggered() ), this, SLOT( OnImportTilesPNGAdv() ) );
	connect( m_pActImportTiles, SIGNAL( triggered() ), this, SLOT( OnImportTiles() ) );
	connect( m_pActGroupTiles, SIGNAL( triggered() ), this, SLOT( OnGroupTiles() ) );

	connect( m_pActCopyTiles, SIGNAL( triggered() ), this, SLOT( OnCopyTiles() ) );
	connect( m_pActPasteTiles, SIGNAL( triggered() ), this, SLOT( OnPasteTiles() ) );
	connect( m_pActPasteTilesColors, SIGNAL( triggered() ), this, SLOT( OnPasteTilesColors() ) );
	connect( m_pActUndo, SIGNAL( triggered() ), this, SLOT( OnUndo() ) );
	
	connect( m_pActNewSprites, SIGNAL( triggered() ), this, SLOT( OnNewSprites() ) );
	connect( m_pActLoadSprites, SIGNAL( triggered() ), this, SLOT( OnLoadSprites() ) );
	connect( m_pActSaveSprites, SIGNAL( triggered() ), this, SLOT( OnSaveSprites() ) );
	connect( m_pActSaveAsSprites, SIGNAL( triggered() ), this, SLOT( OnSaveAsSprites() ) );
	connect( m_pActCloseSprites, SIGNAL( triggered() ), this, SLOT( OnCloseSprites() ) );
	connect( m_pActExportSprites, SIGNAL( triggered() ), this, SLOT( OnExportSprites() ) );
	
	connect( m_pActAbout, SIGNAL( triggered() ), this, SLOT( OnAbout() ) );
	
	m_pMenu = menuBar()->addMenu( tr( "&Project" ) );
	m_pMenu->addAction( m_pActNewProject );
	m_pMenu->addAction( m_pActLoadProject );
	m_pMenu->addAction( m_pActSaveProject );
	m_pMenu->addAction( m_pActSaveAsProject );
	m_pMenu->addAction( m_pActCloseProject );
	m_pMenu->addAction( m_pActImportPolkaProject );
	m_pMenu->addAction( m_pActExportPolkaProject );
	m_pMenu->addAction( m_pActQuit );
	
//	m_pMenu = menuBar()->AddMenu( tr( "Edit" ) );
	//m_pMenu->addAction( m_pActCopy );
	//m_pMenu->addAction( m_pActPaste );
	
	m_pMenu = menuBar()->addMenu( tr( "Screen" ) );
	m_pMenu->addAction( m_pActNewScreen );
	m_pMenu->addAction( m_pActLoadScreen );
	m_pMenu->addAction( m_pActSaveScreen );
	m_pMenu->addAction( m_pActSaveAsScreen );
	m_pMenu->addAction( m_pActExportScreenData );
	m_pMenu->addAction( m_pActExportScreenDataHexa );
	m_pMenu->addAction( m_pActExportScreenBin );
	m_pMenu->addAction( m_pActExportScreenBinPletter );
    m_pMenu->addAction( m_pActExportScreenPNG );
    m_pMenu->addSeparator();
    m_pMenu->addAction( m_pActFillScreen1to255 );

	
	
	m_pMenu = menuBar()->addMenu( tr( "Tiles" ) );
	m_pMenu->addAction( m_pActNewTiles );
	m_pMenu->addAction( m_pActLoadTiles );
	m_pMenu->addAction( m_pActSaveTiles );
	m_pMenu->addAction( m_pActSaveAsTiles );
	m_pMenu->addAction( m_pActLoadTilesLibrary );
	m_pMenu->addAction( m_pActExportTilesData );
	m_pMenu->addAction( m_pActExportTilesDataHexa );
	m_pMenu->addAction( m_pActExportTilesDataHexa );
	m_pMenu->addAction( m_pActExportTilesBin );
	m_pMenu->addAction( m_pActExportTilesBinPletter );
    m_pMenu->addAction( m_pActExportTilesPNG );
    m_pMenu->addAction( m_pActImportTilesPNG );
    m_pMenu->addAction( m_pActImportTilesPNGAdv );
    m_pMenu->addAction( m_pActImportTiles );

	
	m_pMenu = menuBar()->addMenu( tr( "Tiles Tools" ) );
	m_pMenu->addAction( m_pActCopyTiles );
	m_pMenu->addAction( m_pActPasteTiles );
	m_pMenu->addAction( m_pActPasteTilesColors );
	m_pMenu->addAction( m_pActUndo );
	m_pMenu->addAction( m_pActGroupTiles );
	
	m_pMenu = menuBar()->addMenu( tr( "Sprites" ) );
	m_pMenu->addAction( m_pActNewSprites );
	m_pMenu->addAction( m_pActLoadSprites );
	m_pMenu->addAction( m_pActSaveSprites );
	m_pMenu->addAction( m_pActSaveAsSprites );
	m_pMenu->addAction( m_pActCloseSprites );
	m_pMenu->addAction( m_pActExportSprites );
	
	m_pMenu = menuBar()->addMenu( tr( "About" ) );
	m_pMenu->addAction( m_pActAbout );

	m_pWks = new QWorkspace;
	setCentralWidget( m_pWks );

	m_pScreen = NULL;
	m_pSprites = NULL;

	resize( 450, 600 );

	m_ProjectFile = "";
	m_ScreenFile = "";
	m_TilesFile = "";

	m_LastPath = QDir::currentPath();

	setWindowTitle( QString( "nMSXtiles - v%1" ).arg( m_Version ) );
        setWindowIcon( QIcon(QString::fromUtf8(":/new/res/icono.png") ) );

	EnableMenuItems();
}


void CMainWindow::OnNewProject()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsProject();	
	
	delete m_pScreen;
	
	m_pScreen = new CScreenW();
	m_pScreen->Initialize();
	m_pWks->addWindow( m_pScreen, Qt::Dialog );
	m_pScreen->show();
	m_pScreen->NewTiles();
	m_pScreen->NewScreen();

	m_ProjectFile = "";
	m_ScreenFile = "";
	m_TilesFile = "";

	EnableMenuItems();
}

void CMainWindow::OnLoadProject()
{
	int i;
	QString relPath;
	QFile hFile;
	QString fileName;

	
	if( m_pScreen ) OnCloseProject();
	
	fileName = QFileDialog::getOpenFileName( this, "Project - Load", m_LastPath, "nMSXtiles Project (*.prj);;All files (*.*)" );

	if( fileName == "" ) return;

	m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

	m_ProjectFile = fileName;
	
	hFile.setFileName( m_ProjectFile );
	hFile.open( QIODevice::ReadOnly );

	QTextStream str( &hFile );

	if( hFile.atEnd() ) 
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}
	
	if( str.readLine() != "PROJECT" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}

	if( str.readLine() != "SCREEN" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}

	i = m_ProjectFile.lastIndexOf( "/" );
	
	relPath = m_ProjectFile.left( i );
		
	m_ScreenFile = str.readLine();
	
	if( m_ScreenFile.lastIndexOf( "/" ) == -1 ) m_ScreenFile = relPath + QString( "/" ) + m_ScreenFile;
	
	m_pScreen = new CScreenW();
	m_pScreen->Initialize();
	m_pWks->addWindow( m_pScreen, Qt::Dialog );
	m_pScreen->show();

	m_pScreen->LoadScreen( m_ScreenFile );
	
	if( str.readLine() != "TILES" )
	{
		hFile.close();
		QMessageBox::critical( this, "Error", "Unrecognized format file" );
		return;
	}
	
	m_TilesFile = str.readLine();
	
	if( m_TilesFile.lastIndexOf( "/" ) == -1 ) m_TilesFile = relPath + QString( "/" ) + m_TilesFile;
	
	m_pScreen->LoadTiles( m_TilesFile );

	if( str.readLine() == "ONEBANK" ) m_pScreen->SetOneBank( true );
	else m_pScreen->SetOneBank( false );

	hFile.close();
	
	EnableMenuItems();
}

void CMainWindow::OnImportPolkaProject()
{
        QString relPath;
        QFile hFile;
        QString fileName;


        if( m_pScreen ) OnCloseProject();

        fileName = QFileDialog::getOpenFileName( this, "Import Polka! project", m_LastPath, "Polka! project (*.ppr);;All files (*.*)" );

        if( fileName == "" ) return;

        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
		m_pScreen = new CScreenW();
		m_pScreen->Initialize();
		m_pWks->addWindow( m_pScreen, Qt::Dialog );
		m_pScreen->show();
//        m_pScreen->ImportPolkaScreen( fileName );
        m_pScreen->ImportPolkaTiles( fileName );

        EnableMenuItems();
}

void CMainWindow::OnExportPolkaProject()
{
	QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Export Polka! project", m_LastPath, "Polka! project (*.ppr);;All files (*.*)" );

	if( fileName > "" )
	{
		m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
		m_pScreen->ExportPolkaTiles( fileName );
	}
}

void CMainWindow::OnSaveProject()
{
	if( m_ProjectFile == "" ) OnSaveAsProject();
	else SaveProject();
}

void CMainWindow::OnSaveAsProject()
{
	QString fileName;

	
	if( m_ScreenFile == "" ) OnSaveAsScreen();
	else m_pScreen->SaveScreen( m_ScreenFile );
	
	if( m_TilesFile == "" ) OnSaveAsTiles();
	else m_pScreen->SaveTiles( m_TilesFile );
	
	if( m_ScreenFile == "" || m_TilesFile == "" ) 
	{
		QMessageBox::question( this, "Project not saved!", "Warning" );
		return; 
	}

        m_ProjectFile = QFileDialog::getSaveFileName( this, "Project - Save as", m_LastPath, "nMSXtiles Project (*.prj);;All files (*.*)" );

	qDebug()<<"CMainWindow::OnSaveASProject ProjectFile="<<m_ProjectFile;

	m_LastPath = m_ProjectFile.left( m_ProjectFile.lastIndexOf( "/" ) );

	if( m_ProjectFile > "" ) SaveProject();
	else QMessageBox::question( this, "Project not saved!", "Warning" );	
}

void CMainWindow::SaveProject()
{
	int i;
	QFile hFile;
	QString projectFile;
	QString tilesFile;
	QString screenFile;
	
	if( m_ProjectFile == "" ) OnSaveAsProject();
	else
	{	
		OnSaveScreen();
		OnSaveTiles();

		i = m_ScreenFile.lastIndexOf( "/" );
		if( i == -1 ) screenFile = m_ScreenFile;
		else screenFile = m_ScreenFile.left( i );
		
		i = m_TilesFile.lastIndexOf( "/" );
		if( i == -1 ) tilesFile = m_TilesFile;
		else tilesFile = m_TilesFile.left( i );

		i = m_ProjectFile.lastIndexOf( "/" );
		if( i == -1 ) projectFile = m_ProjectFile;
		else projectFile = m_ProjectFile.left( i );

		if( screenFile == projectFile && tilesFile == projectFile )
		{
			if( i == -1 )
			{
				projectFile = m_ProjectFile;
				screenFile = m_ScreenFile;
				tilesFile = m_TilesFile;
			}
			else
			{
				projectFile = m_ProjectFile.right( m_ProjectFile.size() - m_ProjectFile.lastIndexOf( "/" ) - 1 );
				screenFile = m_ScreenFile.right( m_ScreenFile.size() - m_ScreenFile.lastIndexOf( "/" ) - 1 );
				tilesFile = m_TilesFile.right( m_TilesFile.size() - m_TilesFile.lastIndexOf( "/" ) - 1 );
			}
		}
		else
		{
			projectFile = m_ProjectFile;
			screenFile = m_ScreenFile;
			tilesFile = m_TilesFile;
		}
		
		hFile.setFileName( m_ProjectFile );
		hFile.open( QIODevice::WriteOnly );

		QTextStream str( &hFile );
		
		str<<"PROJECT"<<endl;
		str<<"SCREEN"<<endl;
		str<<screenFile<<endl;
		str<<"TILES"<<endl;
		str<<tilesFile<<endl;

		if( m_pScreen->GetOneBank() ) str<<"ONEBANK"<<endl;
		else str<<"ALLBANKS"<<endl;

		hFile.flush();
		hFile.close();
	}
}

void CMainWindow::OnCloseProject()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveProject();	

	m_pScreen->Close();
	delete m_pScreen;
	m_pScreen = NULL;

	EnableMenuItems();
}

void CMainWindow::OnNewScreen()
{
	OnSaveScreen();
	m_pScreen->NewScreen();
}

void CMainWindow::OnLoadScreen()
{
	QFile hFile;
	QString line;
	QString fileName;


        fileName = QFileDialog::getOpenFileName( this, "Screen - Load", m_LastPath, "nMSXtiles Screen (*.scr);;All files (*.*)" );

	if( fileName == "" ) return;

	m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
	
	m_ScreenFile = fileName;

	m_pScreen->LoadScreen( m_ScreenFile );
}


void CMainWindow::OnSaveScreen()
{
	if( m_ScreenFile == "" ) OnSaveAsScreen();
	else m_pScreen->SaveScreen( m_ScreenFile );
}

void CMainWindow::OnSaveAsScreen()
{
	QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Screen - Save as", m_LastPath, "nMSXtiles Screen (*.scr);;All files (*.*)" );

	if( fileName > "" ) 
	{
		m_ScreenFile = fileName;
		m_pScreen->SaveScreen( m_ScreenFile );
		m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
	}
}

void CMainWindow::OnExportScreenData()
{
	QString fileName;
    CDlgSubscreen *pSub;


    pSub = new CDlgSubscreen( this );

    if( pSub->exec() == QDialog::Rejected ) return;

	
    fileName = QFileDialog::getSaveFileName( this, "Screen - Export to ASM as", m_LastPath );

    if( fileName > "" )
    {
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pScreen->ExportScreenData( fileName, false, pSub->get_DefX(), pSub->get_DefY(), pSub->get_DefWidth(), pSub->get_DefHeight() );
    }
}

void CMainWindow::OnExportScreenDataHexa()
{
	QString fileName;
    CDlgSubscreen *pSub;


    pSub = new CDlgSubscreen( this );

    if( pSub->exec() == QDialog::Rejected ) return;

    fileName = QFileDialog::getSaveFileName( this, "Screen - Export to ASM as", m_LastPath );

    if( fileName > "" )
    {
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pScreen->ExportScreenData( fileName, true, pSub->get_DefX(), pSub->get_DefY(), pSub->get_DefWidth(), pSub->get_DefHeight() );
    }
}

void CMainWindow::OnExportScreenBin()
{
        QString fileName;
        CDlgSubscreen *pSub;


        pSub = new CDlgSubscreen( this );

        if( pSub->exec() == QDialog::Rejected ) return;

		fileName = QFileDialog::getSaveFileName( this, "Screen - Export to bin data as", m_LastPath );

        if( fileName > "" )
        {
            m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
            m_pScreen->ExportScreenBin( fileName, pSub->get_DefX(), pSub->get_DefY(), pSub->get_DefWidth(), pSub->get_DefHeight() );
        }
}

void CMainWindow::OnExportScreenBinPletter()
{
		QString fileName;
        CDlgSubscreen *pSub;


        pSub = new CDlgSubscreen( this );

        if( pSub->exec() == QDialog::Rejected ) return;

		fileName = QFileDialog::getSaveFileName( this, "Screen - Export to bin data compressed with Pletter as", m_LastPath );

		if( fileName > "" )
		{
			m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
            m_pScreen->ExportScreenBinPletter( fileName, pSub->get_DefX(), pSub->get_DefY(), pSub->get_DefWidth(), pSub->get_DefHeight() );
		}
}

void CMainWindow::OnExportScreenPNG() {
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Screen - Export PNG file as", m_LastPath );

    if( fileName > "" )
    {
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pScreen->ExportScreenPNG( fileName );
    }
}

void CMainWindow::OnFillScreen1to255()
{
    if( QMessageBox::question( this, "Confirm", "Fill Screen with tiles from 1 to 255", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )
    {
        m_pScreen->Fill1to255();
    }

}

void CMainWindow::OnQuit()
{
	if( m_pScreen && QMessageBox::question( this, "Confirm", "Save Project?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsProject();	
	
	if( m_pScreen ) m_pScreen->Close();	
	
	close();
}

void CMainWindow::OnAbout()
{
	QMessageBox::about( this, "nMSXtiles", QString( "Screens editor for MSX screen 2\n\nVersion %1\n\nRamon de las Heras Gomiz\nwww.pentacour.com" ).arg( m_Version ) );
}

void CMainWindow::OnNewTiles()
{
	OnSaveTiles();
	m_pScreen->NewTiles();
}

void CMainWindow::OnLoadTilesLibrary()
{
    FDlgLoadBiblio *pDlg;

    pDlg = new FDlgLoadBiblio( m_LastPath );
    if( !pDlg->exec() ) return;

    if( pDlg->get_FileName() == "" ) return;

    m_LastPath = pDlg->get_FileName().left( pDlg->get_FileName().lastIndexOf( "/" ) );

    m_pScreen->LoadTilesLibrary( pDlg->get_FileName(), pDlg->get_BankOr(), pDlg->get_XOr(), pDlg->get_YOr(), pDlg->get_Widht(), pDlg->get_Height(), pDlg->get_BankDest(), pDlg->get_XDest(), pDlg->get_YDest() );
}


void CMainWindow::OnLoadTiles()
{
	QFile hFile;
	QString line;
	QString fileName;


        fileName = QFileDialog::getOpenFileName( this, "Tiles - Load", m_LastPath, "nMSXtiles Tiles (*.til);;All files (*.*)" );

	if( fileName == "" ) return;
	
	m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

	m_TilesFile = fileName;

	m_pScreen->LoadTiles( m_TilesFile );
}

void CMainWindow::OnSaveTiles()
{
	if( m_TilesFile == "" ) OnSaveAsTiles();
	else m_pScreen->SaveTiles( m_TilesFile );
}

void CMainWindow::OnSaveAsTiles()
{
	QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Tiles - Save as",  m_LastPath, "nMSXtiles Tiles (*.til);;All files (*.*)" );

	if( fileName > "" ) 
	{
		m_TilesFile = fileName;
		m_pScreen->SaveTiles( m_TilesFile );
		m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
	}
}

void CMainWindow::OnGroupTiles()
{
	m_pScreen->GroupTiles();
}

void CMainWindow::OnExportTilesPNG()
{
    QString fileName;


    fileName = QFileDialog::getSaveFileName( this, "Tiles - Export PNG as", m_LastPath );

    if( fileName > "" )
    {
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ));
        m_pScreen->ExportTilesPNG( fileName );
    }
}

void CMainWindow::OnExportTilesData()
{
	QString fileName;
	
	
        fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to ASM as", m_LastPath );
	
        if( fileName > "" )
        {
            m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
			m_pScreen->ExportTilesData( fileName, false );
        }
}

void CMainWindow::OnExportTilesDataHexa()
{
	QString fileName;


		fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to ASM as", m_LastPath );

		if( fileName > "" )
		{
			m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
			m_pScreen->ExportTilesData( fileName, true );
		}
}

void CMainWindow::OnExportTilesBin()
{
        QString fileName;


		fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to bin data as", m_LastPath );

        if( fileName > "" )
        {
            m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
            m_pScreen->ExportTilesBin( fileName );
        }

}

void CMainWindow::OnExportTilesBinPletter()
{
		QString fileName;


		fileName = QFileDialog::getSaveFileName( this, "Tiles - Export to bin data compressed with Pletter as", m_LastPath );

		if( fileName > "" )
		{
			m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
			m_pScreen->ExportTilesBinPletter( fileName );
		}

}

void CMainWindow::OnImportTilesPNG()
{
    QString fileName;


    fileName = QFileDialog::getOpenFileName( this, "Tiles - Import PNG file created with nMSXtiles", m_LastPath, "Image (*.png);;All files (*.*)"  );

    if( fileName == ""  )
    {
        QMessageBox::critical( this, "Error", "No file selected!" );
        return;
    }

    m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

    if( !m_pScreen->ImportTiles( fileName ) )
    {
        QMessageBox::critical( this, "Error", "Not supported format file" );
    }
}

void CMainWindow::OnImportTilesPNGAdv()
{
    QString fileName;


    fileName = QFileDialog::getOpenFileName( this, "Tiles - Import PNG file withour palette", m_LastPath, "Image (*.png);;All files (*.*)"  );

    if( fileName == ""  )
    {
        QMessageBox::critical( this, "Error", "No file selected!" );
        return;
    }

    m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

    if( !m_pScreen->ImportTilesPNGAdv( fileName ) )
    {
        QMessageBox::critical( this, "Error", "Not supported format file" );
    }
}

void CMainWindow::OnImportTiles()
{
	QString fileName;
	QString fileNamePalette;
	

	QMessageBox::information( this, "", "First you will be prompted for an Image with Palette (see manual). Then you will be prompted for the Tiles Image" );
		
        fileNamePalette = QFileDialog::getOpenFileName( this, "Tiles - Import Image with Palette", m_LastPath, "Palette (*.png);;All files (*.*)" );
	
	if( fileNamePalette == "" )
	{
		QMessageBox::critical( this, "Error", "No palette, no image!" );
		return;
	}
	
        m_LastPath = fileNamePalette.left( fileNamePalette.lastIndexOf( "/" ) );

        fileName = QFileDialog::getOpenFileName( this, "Tiles - Import Image with Tiles", m_LastPath, "Image (*.png);;All files (*.*)"  );
	
	if( fileName == ""  )  
	{
		QMessageBox::critical( this, "Error", "No file selected!" );
		return;
	}
	
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );

	if( !m_pScreen->ImportTiles( fileName, fileNamePalette ) )
	{
		QMessageBox::critical( this, "Error", "Not supported format file" );
	}
}

void CMainWindow::OnUndo()
{
	m_pScreen->Undo();
}

void CMainWindow::OnCopyTiles()
{
	m_pScreen->CopyTile();
}

void CMainWindow::OnPasteTiles()
{
	m_pScreen->PasteTile();
}

void CMainWindow::OnPasteTilesColors()
{
	m_pScreen->PasteTileColors();
}

void CMainWindow::OnNewSprites()
{
	if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsSprites();	
	
	delete m_pSprites;
	
	m_pSprites= new CSpritesW();
	m_pSprites->Initialize();
	m_pWks->addWindow( m_pSprites, Qt::Dialog );
	m_pSprites->show();
	m_pSprites->NewSprites();

	m_SpritesFile = "";

	EnableMenuItems();
}

void CMainWindow::OnLoadSprites()
{
	QFile hFile;
	QString line;
	QString fileName;

	
	if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveAsSprites();	

        fileName = QFileDialog::getOpenFileName( this, "Sprite - Load", m_LastPath, "nMSXtiles Sprite (*.spt);;All files (*.*)");

	if( fileName == "" ) return;

        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
	
	m_SpritesFile = fileName;
	
	if( m_pSprites ) delete m_pSprites; 
	
	m_pSprites= new CSpritesW();
	m_pSprites->Initialize();
	m_pWks->addWindow( m_pSprites, Qt::Dialog );
	m_pSprites->show();
	m_pSprites->LoadSprites( m_SpritesFile );

	EnableMenuItems();
}

void CMainWindow::OnSaveSprites()
{
	if( m_SpritesFile == "" ) OnSaveAsSprites();
	else m_pSprites->SaveSprites( m_SpritesFile );
}

void CMainWindow::OnSaveAsSprites()
{
	QString fileName;


        fileName = QFileDialog::getSaveFileName( this, "Sprites - Save as", m_LastPath, "nMSXtiles Sprite (*.spt);;All files (*.*)" );

	if( fileName > "" ) 
	{
            m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
            m_SpritesFile = fileName;
            m_pSprites->SaveSprites( m_SpritesFile );
	}
}

void CMainWindow::OnCloseSprites()
{
	if( m_pSprites && QMessageBox::question( this, "Confirm", "Save Sprites?", QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes )	OnSaveSprites();	

	m_pSprites->Close();

	delete m_pSprites;
	m_pSprites = NULL;

	EnableMenuItems();

}

void CMainWindow::OnExportSprites()
{
    QString fileName;
	
	
    fileName = QFileDialog::getSaveFileName( this, "Sprites - Export as", m_LastPath );
	
    if( fileName > "" )
    {
        m_LastPath = fileName.left( fileName.lastIndexOf( "/" ) );
        m_pSprites->ExportSprites( fileName );
    }
}

void CMainWindow::EnableMenuItems()
{

	m_pActLoadProject->setEnabled( true );
	m_pActSaveProject->setEnabled( m_pScreen != NULL );
	m_pActSaveAsProject->setEnabled( m_pScreen != NULL );
	m_pActCloseProject->setEnabled( m_pScreen != NULL );
        m_pActImportPolkaProject->setEnabled( true );
        m_pActExportPolkaProject->setEnabled( m_pScreen != NULL );

	m_pActNewScreen->setEnabled( m_pScreen!= NULL );
	m_pActLoadScreen->setEnabled( m_pScreen != NULL );
	m_pActSaveScreen->setEnabled( m_pScreen != NULL );
	m_pActSaveAsScreen->setEnabled( m_pScreen != NULL );
	m_pActExportScreenData->setEnabled( m_pScreen != NULL );
        m_pActExportScreenDataHexa->setEnabled( m_pScreen != NULL );
	m_pActExportScreenBin->setEnabled( m_pScreen != NULL );
	m_pActExportScreenBinPletter->setEnabled( m_pScreen != NULL );
        m_pActFillScreen1to255->setEnabled( m_pScreen != NULL );

        m_pActImportTiles->setEnabled( m_pScreen != NULL );
        m_pActNewTiles->setEnabled( m_pScreen != NULL );
	m_pActLoadTiles->setEnabled( m_pScreen != NULL );
	m_pActSaveTiles->setEnabled( m_pScreen != NULL );
	m_pActSaveAsTiles->setEnabled( m_pScreen != NULL );
	m_pActLoadTilesLibrary->setEnabled( m_pScreen != NULL );
	m_pActExportTilesData->setEnabled( m_pScreen != NULL );
	m_pActExportTilesBin->setEnabled( m_pScreen != NULL );
	m_pActExportTilesBinPletter->setEnabled( m_pScreen != NULL );
	m_pActCopyTiles->setEnabled( m_pScreen != NULL );
	m_pActPasteTiles->setEnabled( m_pScreen != NULL );
	m_pActPasteTilesColors->setEnabled( m_pScreen != NULL );
	m_pActUndo->setEnabled( m_pScreen != NULL );
        m_pActExportTilesDataHexa->setEnabled( m_pScreen );
        m_pActImportTilesPNG->setEnabled( m_pScreen );
        m_pActExportTilesPNG->setEnabled( m_pScreen );
        m_pActGroupTiles->setEnabled( m_pScreen );
	
	m_pActNewSprites->setEnabled( true );
	m_pActLoadSprites->setEnabled( true );
	m_pActSaveSprites->setEnabled( m_pSprites != NULL );
	m_pActSaveAsSprites->setEnabled( m_pSprites != NULL );
	m_pActCloseSprites->setEnabled( m_pSprites != NULL );
	m_pActExportSprites->setEnabled( m_pSprites != NULL );
}

void CMainWindow::closeEvent(QCloseEvent *event)
{
	OnQuit();
}

